package service;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.asm.Type;

import vo.Product;
import vo.Producttype;
import vo.Spec;
import dao.BaseDao;
import dao.ProductDao;
import dao.ProductTypeDao;
import dao.SpecDao;

public class ProductServiceImpl implements ProductService{
	ProductDao productDao;
	ProductTypeDao typeDao;
	SpecDao specDao;
	List list = new ArrayList();
	List<Producttype> ptype = new ArrayList<Producttype>();
	//添加产品
	public String addProduct(Product product, Producttype type, int price1,
			int price2, int price3){
		String result = "";
		Date date=new Date();
		Timestamp createdTime = new Timestamp(date.getTime());
		String productNo = "pr"+date.getTime();
		product.setCreatedTime(createdTime);
		product.setUpdateTime(createdTime);
		type = getTypeByName(type);
		product.setProducttype(getTypeByName(type));
		product.setProductNo(productNo);
		if(checkName(product.getProductName())){
			result = "此产品已存在";
		}else{
			if(productDao.save(product)){
				setSpec(productNo,price1,price2,price3);
				result =  "添加成功";
			}else{
				result =  "添加失败";
			}			
		}
		return result;
	}
	
	/**
	 * @param productNo
	 * @param price1
	 * @param price2
	 * @param price3
	 */
	private void setSpec(String productNo, int price1, int price2, int price3) {
		/*Product product = getProductByNo(productNo);
		System.out.println(product.toString());
		Spec spec1 = new Spec(product,price1,"大");
		Spec spec2 = new Spec(product,price2,"中");
		Spec spec3 = new Spec(product,price3,"小");
		dao.save(spec1);
		dao.save(spec2);
		dao.save(spec3);*/
		String sql1 = "insert into spec(productNo,price,size) value('"+productNo+"','"+price1+"','大杯')";
		String sql2 = "insert into spec(productNo,price,size) value('"+productNo+"','"+price2+"','中杯')";
		String sql3 = "insert into spec(productNo,price,size) value('"+productNo+"','"+price3+"','小杯')";
		productDao.excuteBySql(sql1);
		productDao.excuteBySql(sql2);
		productDao.excuteBySql(sql3);
	}

	//删除产品
	public String deleteProduct(Product product){
		product = getProductByNo(product.getProductNo());
		String sql = "delete from spec where productNo='"+product.getProductNo()+"'";
		String sql2 = "delete from product where productNo='"+product.getProductNo()+"'";
		int result = productDao.excuteBySql(sql);
		System.out.println(result);
		if(productDao.excuteBySql(sql2)>0){
			return "删除成功";
		}else{
			return "删除失败";
		}
	}
	
	//查询产品
	public List selectProduct(String key){
		String hql = "from Product where "
				+ "price like '%"+key+"%' or productName like '%"+key+"%' "
				+ "or productType like '%"+key+"%'";
		String hql2 = "from Product";
		List list = new ArrayList();
		list = productDao.select(hql);
		return list;
	}
	
	
	//更新产品
	public String updateProduct(Product product,int price1, int price2, int price3){
		Date date=new Date();
		Timestamp updateTime = new Timestamp(date.getTime());
		String hql = "from Product where productName='"+product.getProductName()+"'";
		if(productDao.select(hql).size()>0){
			return "产品名称已存在";
		}else{
			String sql = "update product set productName='"+product.getProductName()+"',"
					+ "remark='"+product.getRemark()+"',updateTime='"+updateTime+"' "
					+ "where productNo='"+product.getProductNo()+"'";
			productDao.excuteBySql(sql);
			/*Product p = productDao.findById(product.getId());
			p.setUpdateTime(updateTime);
			p.setProductName(product.getProductName());
			p.setRemark(product.getRemark());
			Set specSet = new HashSet();
			specSet.addAll(specDao.findByProductNo(product.getProductNo()));*/
			String sql2 = "update spec set price = case size when '大杯' then '"+price1+"' "
						+ "when '中杯' then '"+price2+"' "
						+ "when '小杯' then '"+price3+"' end"
						+ " where productNo='"+product.getProductNo()+"'";
			specDao.excuteBySql(sql2);
			return "更新成功";
		}
	}
	
	//通过id获取产品所有信息
	public Product getProductByNo(String productNo){
		String hql = "from Product where productNo='"+productNo+"'";
		list = productDao.select(hql);
		System.out.println("getpr"+list.toString());
		Product product = new Product();
		product = (Product)list.get(0);
		return product;
	}
	
	//添加产品类型
	public String addProductType(Producttype type){
		System.out.println(type.getName());
		if(checkType(type.getName())){
			return "该种类已存在";
		}else{
			/*ptype.add(type);*/
			typeDao.save(type);
			return type.getName();
		}
	}
	
	//获取产品类型
	public List getProductType(){
		String hql = "from Producttype";
		list = typeDao.select(hql);
		return list;
	}
	
	//获取产品类型
	public Producttype getTypeByName(Producttype type){
		String hql = "from Producttype where name='"+type.getName()+"'";
		list = typeDao.select(hql);
		Producttype result = (Producttype)list.get(0);
		return result;
	}

	
	//检查产品名字是否存在
	@Override
	public boolean checkName(String productName) {
		String hql = "from Product where productName='"+productName+"'";
		list = productDao.select(hql);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean checkType(String type) {
		list = new ArrayList();
		String sql = "select * from producttype where name='"+type+"'";
		list = typeDao.selectBySql(sql);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public List<Product> getAllProduct() {
		list = productDao.findAll();
		List list2 = new ArrayList();
		for(int i= 0; i < list.size(); i++){
			Product pro = (Product) list.get(i);
			list2.add(getSpec(pro));
		}
		return list2;
	}

	@Override
	public String deleteProductType(int id) {
		String sql2 = "select * from product where productType='"+id+"'";
		if(typeDao.selectBySql(sql2).size()>0){
			return "请先删除该类别下的产品";
		}else{
			String sql = "delete from producttype where id='"+id+"'";
			int result = typeDao.excuteBySql(sql);
			return "删除成功";
		}
		
	}


	@Override
	public Map<String,Object> getSpec(Product product) {
		Map<String,Object> map = new HashMap<String, Object>();
		List<Spec> specList = new ArrayList<Spec>(); 
		specList = specDao.selectBySql("select * from spec where productNo='"+product.getProductNo()+"'");
		map.put("product", product);
		map.put("spec", specList);
		return map;
	}


	@Override
	public List searchProductType(String key) {
		String hql = "from Producttype where name like '%"+key+"%'";
		list = typeDao.select(hql);
		return list;
	}

	public List<Product> getProductByType(Producttype type){
		String sql = "from Product where productType='"+type.getId()+"'";
		List<Product> list2 = new ArrayList<Product>();
		list2 = productDao.select(sql);
		/*List list3 = new ArrayList();
		for(int i= 0; i < list.size(); i++){
			Product pro = (Product) list2.get(i);
			list3.add(getSpec(pro));
		}*/
		return list2;
	}
	
	public ProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public ProductTypeDao getTypeDao() {
		return typeDao;
	}

	public void setTypeDao(ProductTypeDao typeDao) {
		this.typeDao = typeDao;
	}

	public SpecDao getSpecDao() {
		return specDao;
	}

	public void setSpecDao(SpecDao specDao) {
		this.specDao = specDao;
	}

	/* (non-Javadoc)
	 * @see service.ProductService#updateType(vo.Producttype)
	 */
	@Override
	public String updateType(Producttype type) {
		String sql = "update producttype set name='"+type.getName()+"'"
				+ "where id='"+type.getId()+"'";
		if(checkType(type.getName())){
			return "该种类已存在";
		}else{
			if(typeDao.excuteBySql(sql)>0){
				return "更新成功";
			}else{
				return "更新失败";
			}
		}	
	}

}
